#include "wds_ui_pausemenu.h"
#include "gen_game_state.h"
#include "wds_ui_helper.h"
//#include "fig_ios_wrapper.h"


#include "ui/ui_manager.h"
#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "gameflow/ui_state.h"
#include "font/font_manager.h"
#include "graphics/graphics.h"
#include "graphics/graphics_util.h"
#include "application/game_application.h"
#include "application/game_rms.h"
#include "nextpeer/np_manager.h"

#include "util/log.h"
#include "util/profiler.h"

#ifdef _PLATFORM_ANDROID
#include "SDL2/src/core/android/SDL_android.h"
#endif




WDSUIPauseMenu::WDSUIPauseMenu() : 
				WDSUIPopupAnimator("ui/pause_menu.ui","Button_Resume")											
{
   
	if(m_pPopup)
	{
		m_pPopup->SetWidgetFunctor("Button_Settings",UI_DELEGATE(WDSUIPauseMenu, OnButtonSettings));
		m_pPopup->SetWidgetFunctor("Button_GoToMainMenu",UI_DELEGATE(WDSUIPauseMenu, OnButtonGotoMainMenu));
		m_pPopup->SetWidgetFunctor("play_button",UI_DELEGATE(WDSUIPauseMenu, OnButtonResume));

		m_pPopup->SetWidgetFunctor("toFB",UI_DELEGATE(WDSUIPauseMenu, OnButtonGotoFacebook));
		m_pPopup->SetWidgetFunctor("toTwitter",UI_DELEGATE(WDSUIPauseMenu, OnButtonGotoTwitter));
		m_pPopup->SetWidgetFunctor("to2DStar",UI_DELEGATE(WDSUIPauseMenu, OnButtonGoto2DStar));
		
		//m_pPopup->SetupTransition("goto_main_menu", "MainMenu");
		//m_pPopup->SetWidgetPopup("SETTINGS", "settings");

		m_pAnimateWidget = m_pPopup->GetWidget("BG");
		WDSUIPopupAnimatorMgr::Instance().AddPopupAnimator(WDSUIPopupAnimatorMgr::POPUP_NAMES_PAUSEMENU,this);

		ui::UIWidget* pBG = m_pAnimateWidget;
		//WDSUIHelper::ResizeToCurentAspectRatio(pBG);
		
		const math::Vec2 TEMPOS = pBG->GetPosition();
		//WDSUIHelper::RelocateToCurentAspectRatio(m_pPopup->GetWidget("Button_GoToMainMenu"), TEMPOS);
		//WDSUIHelper::RelocateToCurentAspectRatio(m_pPopup->GetWidget("Button_Settings"), TEMPOS);
		//WDSUIHelper::RelocateToCurentAspectRatio(m_pPopup->GetWidget("Button_Resume"), TEMPOS);

		//WDSGame* pGame = GENGameState::GetGame()->GetGameMatch5();
		//pGame->SetPausePopup(m_pPopup);

		const float AspectRatioCreated = 1280.0f / 720.0f;
		const float AsepctRatioCurrent = (float)GetGraphics()->GetScreenHeight() / (float)GetGraphics()->GetScreenWidth();
		if(AspectRatioCreated > AsepctRatioCurrent)
		{
			const float NewWScale =  AsepctRatioCurrent / AspectRatioCreated;	
			math::Vec2 TEMPV = pBG->GetSize();
			pBG->SetSize( math::Vec2(TEMPV.x * NewWScale, TEMPV.y));
		}

	}
	
	m_Type = ANIMATION_MSGPOPUP;
}


void DELEGATE_CALL		WDSUIPauseMenu::OnButtonSettings(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	WDSUIPopupAnimatorMgr::Instance().Show(WDSUIPopupAnimatorMgr::POPUP_NAMES_SETTING);
	m_pPopup->Hide(NULL);
}


void DELEGATE_CALL		WDSUIPauseMenu::OnButtonGotoMainMenu(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	m_pPopup->Hide(NULL);
	gameflow::UIPopupManager::Instance().StartYesNo("LOC_SURE_MAIN", 
													UI_DELEGATE(WDSUIPauseMenu, OnYesGoToMainMenu), UI_DELEGATE(WDSUIPauseMenu, OnButtonResume));		
	WDSUIPopupAnimatorMgr::Instance().Show(WDSUIPopupAnimatorMgr::POPUP_NAMES_YESNO);

	
}

void DELEGATE_CALL		WDSUIPauseMenu::OnYesGoToMainMenu(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	if(g_pApp && g_pApp->GetRMS())
		g_pApp->GetRMS()->Save();

	NextPeerManager::Instance().ReportForfeitForCurrentTournament();
	
	gameflow::GameFlowManager::Instance().SetTransitionEffect("fade","MainMenu","",NULL);

}


void DELEGATE_CALL		WDSUIPauseMenu::OnButtonResume(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	m_pPopup->Hide(NULL);	
}

void DELEGATE_CALL		WDSUIPauseMenu::OnButtonGotoFacebook(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{

    //FIGIOSWrapper::Instance().FIG_GotoWebSite("http://www.facebook.com/2dstar");
   
#ifdef _PLATFORM_ANDROID
    //Android_JNI_GoToURL((int)ANDROID_JNI_URL_FACEBOOK);
#endif

}

void DELEGATE_CALL		WDSUIPauseMenu::OnButtonGotoTwitter(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{

    //FIGIOSWrapper::Instance().FIG_GotoWebSite("http://www.twitter.com/2dstar");
#ifdef _PLATFORM_ANDROID
    //Android_JNI_GoToURL((int)ANDROID_JNI_URL_TWITTER);
#endif

}
	
void DELEGATE_CALL		WDSUIPauseMenu::OnButtonGoto2DStar(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{

    //FIGIOSWrapper::Instance().FIG_GotoWebSite("http://www.2dstar.com");
#ifdef _PLATFORM_ANDROID
    //Android_JNI_GoToURL((int)ANDROID_JNI_URL_2DSTAR);
#endif

}

//void DELEGATE_CALL		FIGUIPauseMenu::OnButtonFullVersion(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
//{
//	FIGUIRemoveAd::Instance().Show(NULL);
//}

bool	WDSUIPauseMenu::KeyPressed(input::Keyboard::KEY* pKey)
{
	if(m_pPopup->IsActive() && pKey->KeyID == KEY_ESCAPE)
	{
		Hide();
		m_pPopup->Hide(NULL);	
		return true;
	}
	return false;
}



